package com.iot.ic.controller;

import com.iot.ic.base.BaseController;
import com.iot.ic.common.Options;
import com.iot.ic.common.Response;
import com.iot.ic.common.ReturnCode;
import com.iot.ic.model.IllegalInfo;
import com.iot.ic.service.IllegalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;

/**
 * Created by xiuge on 2017/7/6.
 */
@RestController
@Api(description = "违规行为相关接口")
public class IllegalController extends BaseController{
	
	@Autowired
	private IllegalService illegalService;

	@ApiOperation(value = "增加违规行为信息", notes = "")
	@PostMapping(value = "/users/{userId}/illegal")
	public Response addIllegalInfos(@PathVariable(value = "userId") Integer userId, @RequestBody @Valid IllegalInfo illegalInfo) {
	    Response response=new Response();
	    illegalInfo.setCreateTime(new Date());
	    illegalInfo.setDelFlag(Options.NotDel.getCode());
	    int result = illegalService.addIllegalInfo(illegalInfo);
	    if (result==1)
	        response.setInfos(ReturnCode.No_AddIllegalOK);
	    else
	        response.setInfos(ReturnCode.Err_AddIllegalFailor);
	    return response;
	}
	
	@ApiOperation(value = "根据ID获得违章行为信息", notes = "")
	@RequestMapping(value="/users/{userId}/illegal/{illegalId}", method=RequestMethod.GET)
	public Response getIllegalInfoById(@PathVariable(value = "userId") Integer userId, 
			@PathVariable(value = "illegalId") Integer illegalId) {
	    Response response=new Response();
	    IllegalInfo  illegalInfo = illegalService.getIllegalInfoById(illegalId); 
	    response.setResult(illegalInfo);
	    return response;
	}
	
	@ApiOperation(value = "根据ID更新违章行为信息", notes = "")
	@RequestMapping(value="/users/{userId}/illegal/{illegalId}", method=RequestMethod.PUT)
	public Response updateIllegalInfo(@PathVariable(value = "userId") Integer userId, 
			@PathVariable(value = "illegalId") Integer illegalId,
			@RequestBody @Valid IllegalInfo illegalInfo) {
	    Response response=new Response();
	    illegalInfo.setIllegalId(illegalId);
	    int result = illegalService.updateIllegalInfo(illegalInfo);
	    if (result==1)
	        response.setInfos(ReturnCode.No_UpdateIllegalOK);
	    else
	        response.setInfos(ReturnCode.Err_UpdateIllegalFailor);
	    return response;
	}
	
	@ApiOperation(value = "删除违章行为信息", notes = "")
	@RequestMapping(value="/users/{userId}/illegal/{illegalId}", method=RequestMethod.DELETE)
	public Response deleteIllegalInfo(@PathVariable(value = "userId") Integer userId, 
			@PathVariable(value = "illegalId") Integer illegalId) {
	    Response response=new Response();
	    int result = illegalService.deleteIllegalInfo(illegalId);
	    if (result==1)
	        response.setInfos(ReturnCode.No_DeleteIllegalOK);
	    else
	        response.setInfos(ReturnCode.Err_DeleteIllegalFailor);
	    return response;
	}
}
